//
//  DefaultKeyboardBtn.swift
//  Keyboard
//
//  Created by 周张林 on 17/8/12.
//  Copyright © 2017年 周张林. All rights reserved.
//

import UIKit

protocol DefaultKeyboardBtnDelegate :class {
    func didClick(_ sender:DefaultKeyboardBtn)->Void
}

class DefaultKeyboardBtn:UIButton
{
    weak var kbDelegate:DefaultKeyboardBtnDelegate?
    
    
    static func newButton(title:String,tag:Int, kbDelegate:DefaultKeyboardBtnDelegate? , fontSize:CGFloat = 22 )->DefaultKeyboardBtn
    {
        let btn = DefaultKeyboardBtn(type: .custom)
        btn.kbDelegate = kbDelegate
        btn.layer.cornerRadius = 5
        btn.clipsToBounds = true
        btn.tag = tag
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: fontSize)
        btn.setTitleColor(UIColor.white, for: .normal)
//        btn.setTitleColor(UIColor.blue, for: .highlighted)
        btn.setTitle(title, for: .normal)
        btn.backgroundColor = UIColor(white: 1, alpha: 0.6)
        if kbDelegate != nil {
            btn.addTarget(btn, action: #selector(btn.onClick), for: .touchUpInside)
        }
        return btn
    }
    
    override func setTitle(_ title: String?, for state: UIControlState)
    {
        UIView.setAnimationsEnabled(false)
        super.setTitle(title, for: state)
        UIView.setAnimationsEnabled(true)
    }
    
    @objc func onClick()->Void{
        kbDelegate?.didClick(self)
    }
    
    deinit {
        kbDelegate = nil
    }
}
